package com.example.timernote

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class RvAdapter(var list: MutableList<BloodPressureInfo>, var context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>(){

    val inflater: LayoutInflater = LayoutInflater.from(context)

    override fun getItemCount(): Int = list.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var view: View = inflater.inflate(R.layout.item_blood_presure_layout,parent,false)
        return ItemHolder(view)
    }

    var STD_LOW:Int
    var STD_HIGH:Int
    var STD_RATE:Int

    init {
        STD_LOW = BloodPressureInfoService.standardValue(BloodPressureInfoService.STD_LOW)
        STD_HIGH = BloodPressureInfoService.standardValue(BloodPressureInfoService.STD_HIGH)
        STD_RATE = BloodPressureInfoService.standardValue(BloodPressureInfoService.STD_RATE)
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as ItemHolder
        if (list[position].low.toInt()>STD_LOW){
            vh.low.setTextColor(Color.RED)
        }else{
            vh.low.setTextColor(Color.GRAY)
        }

        if (list[position].high.toInt()>STD_HIGH){
            vh.high.setTextColor(Color.RED)
        }else{
            vh.high.setTextColor(Color.GRAY)
        }

        if (list[position].rate.toInt()>STD_RATE
            ){
            vh.rate.setTextColor(Color.RED)
        }else{
            vh.rate.setTextColor(Color.GRAY)
        }

        vh.low.text = list[position].low
        vh.high.text = list[position].high
        vh.rate.text = list[position].rate
        vh.date.text = list[position].date
        vh.time.text = list[position].time
    }

    inner class ItemHolder(view: View): RecyclerView.ViewHolder(view){
        var low = view.findViewById<TextView>(R.id.mTvItemBPLow)
        var high = view.findViewById<TextView>(R.id.mTvItemBPHigh)
        var rate = view.findViewById<TextView>(R.id.mTvItemBPRate)
        var date = view.findViewById<TextView>(R.id.mTvItemBPDate)
        var time = view.findViewById<TextView>(R.id.mTvItemBPTime)
    }



}